package com.daxia.bumall.web.controller.admin;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.daxia.bumall.common.HardcoverStyleType;
import com.daxia.bumall.common.RecommonStatus;
import com.daxia.bumall.dto.BrandStyleDTO;
import com.daxia.bumall.service.BrandStyleService;
import com.daxia.bumall.support.Page;
import com.daxia.bumall.web.controller.BaseController;

@Controller
@RequestMapping(value = "/admin/brandStyle", produces="text/html;charset=UTF-8")
public class AdminBrandStyleController extends BaseController {

	@Autowired
	private BrandStyleService brandStyleService;

	@RequestMapping(value = "/detail")
	public String detail(Map<String, Object> map, Long id) {
		if (id != null) {
			BrandStyleDTO dto = brandStyleService.load(id);
			map.put("n", dto);
		}
		
		Map<Long, String> hs = HardcoverStyleType.toMap();
		
		map.put("rs", RecommonStatus.values());
		map.put("hs", hs);
		
		map.put("houseStyles", houseStyleService.findAll());
		
		return "admin/brandStyle/brandStyle_detail";
	}
	
	@ResponseBody
	@RequestMapping(value = "/save")
	//@PreAuthorize("hasRole('brandStyle')")
	public String save(BrandStyleDTO dto) throws Exception {
		if (dto.getId() == null) {
			brandStyleService.create(dto);
		} else {
			BrandStyleDTO old = brandStyleService.load(dto.getId());
			// old.setXXX(dto.getXXX());
			if (1 == 1) {
				throw new RuntimeException("请实现更新逻辑");
			} 
			brandStyleService.updateAllFields(old);
		}
		return ok();
	}

	@ResponseBody
	@RequestMapping(value = "/delete") 
	public String delete(Long[] ids) throws IOException {
		brandStyleService.deleteByIds(ids);
		return ok();
	}
	
	@RequestMapping(value = "/list")
	public String list(BrandStyleDTO dto, Map<String, Object> map, Page page) {
		List<BrandStyleDTO> dtos = brandStyleService.find(dto, page);
		if (dto.getStyleID().longValue() == 0) {
			dto.setStyleID(null);
		}
		map.put("list", dtos);
		map.put("query", dto);
		Map<Long, String> hs = HardcoverStyleType.toMap();
		map.put("hs", hs);
		map.put("rs", RecommonStatus.values());
		return "admin/brandStyle/brandStyle_list";
	}
	
}
